home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / ROM_Kernel_Manuals / Devices / apps / RawtoILBM / RawtoILBM.c
Encoding:
C/C++ Source or Header  |  1992-08-20  |  4.2 KB  |  166 lines

  1. /* RawtoILBM
  2.  * Converts raw file (from ILBMtoRaw) into an ILBM
  3.  * Requires linkage with several iffparse modiules - See Makefile
  4.  */ 
  5.  
  6. #include "iffp/ilbmapp.h"
  7.  
  8. #include <intuition/intuitionbase.h>
  9. #include <workbench/workbench.h>
  10.  
  11. #ifdef LATTICE
  12. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  13. int chkabort(void) { return(0); }  /* really */
  14. #endif
  15.  
  16. char *vers = "\0$VER: RawtoILBM 37.5";
  17. char *Copyright = 
  18.   "RawtoILBM v37.5 - converts raw file to ILBM - Freely Redistributable";
  19. #define MINARGS 6
  20. char *usage = "Usage: RawtoILBM rawname ilbmname width height depth\n";
  21.  
  22. void bye(UBYTE *s,int e);
  23. void cleanup(void);
  24.  
  25. struct Library    *IntuitionBase = NULL;
  26. struct Library    *GfxBase = NULL;
  27. struct Library    *IFFParseBase = NULL;
  28.  
  29. struct ILBMInfo ilbm = {0};
  30.  
  31. USHORT    colortable[MAXAMCOLORREG];
  32.  
  33. BOOL fromWB;
  34.  
  35.  
  36. void main(int argc, char **argv)
  37.     {
  38.     LONG    error = 0L, rawfile, rlen;
  39.     USHORT    width, height, depth, pwidth, pheight, pmode, extra;
  40.     ULONG     plsize;
  41.     char        *rawname,*ilbmname;
  42.     int     k;
  43.  
  44.     fromWB = (argc==0) ? TRUE : FALSE;
  45.  
  46.     if(!(IntuitionBase = OpenLibrary("intuition.library", 0)))
  47.       bye("Can't open intuition library.\n",RETURN_WARN);
  48.       
  49.     if(!(GfxBase = OpenLibrary("graphics.library",0)))
  50.       bye("Can't open graphics library.\n",RETURN_WARN);
  51.  
  52.     if(!(IFFParseBase = OpenLibrary("iffparse.library",0)))
  53.       bye("Can't open iffparse library.\n",RETURN_WARN);
  54.  
  55.     if(!(ilbm.ParseInfo.iff = AllocIFF()))
  56.       bye(IFFerr(IFFERR_NOMEM),RETURN_WARN);
  57.  
  58.     if(argc==MINARGS)                 /* Passed filenames via command line  */
  59.           {
  60.           rawname  = argv[1];
  61.           ilbmname = argv[2];
  62.     width  = atoi(argv[3]);
  63.     height = atoi(argv[4]);
  64.     depth  = atoi(argv[5]);
  65.  
  66.     /* Page width, height, and mode for saved ILBM */
  67.     pwidth  = width  < 320 ? 320 : width;
  68.     pheight = height < 200 ? 200 : height;
  69.     pmode    = pwidth >= 640  ? HIRES : 0L;
  70.     pmode  |= pheight >= 400 ? LACE  : 0L;
  71.  
  72.     plsize = RASSIZE(width,height);
  73.     }
  74.     else
  75.     {
  76.     printf("%s\n%s\n",Copyright,usage);
  77.     bye("\n",RETURN_OK);
  78.     }
  79.      
  80.  
  81.     if(!(rawfile = Open(rawname,MODE_OLDFILE)))
  82.     {
  83.     printf("Can't open raw file '%s'\n",rawname);
  84.     bye(" ",RETURN_WARN);
  85.     }
  86.  
  87.     /*
  88.      * Allocate Bitmap and planes
  89.      */
  90.      extra = depth > 8 ? depth - 8 : 0;
  91.      if(ilbm.brbitmap = AllocMem(sizeof(struct BitMap) + (extra<<2),
  92.                 MEMF_CLEAR))
  93.         {
  94.         InitBitMap(ilbm.brbitmap,depth,width,height);
  95.         for(k=0, error=0, rlen=1; k<depth && (!error) && (rlen >0); k++) 
  96.             {
  97.             if(!(ilbm.brbitmap->Planes[k] = AllocRaster(width,height)))
  98.             error = IFFERR_NOMEM;
  99.             if(! error)
  100.         {
  101.                 BltClear(ilbm.brbitmap->Planes[k], RASSIZE(width,height),0);
  102.         /* Read a plane */
  103.         rlen = Read(rawfile,ilbm.brbitmap->Planes[k],plsize);
  104.                 }
  105.         }
  106.  
  107.     /* get colortable */
  108.     if((!error)&&(rlen > 0))
  109.         rlen=Read(rawfile,colortable,(MIN(1<<depth,MAXAMCOLORREG)<<1));
  110.  
  111.         if((error)||(rlen<=0))
  112.             {
  113.         if(rlen <= 0)      printf("Error loading raw file - check dimensions\n");
  114.             else         printf("Error allocating planes\n");
  115.         }
  116.     else
  117.         {
  118.         error = saveilbm(&ilbm, ilbm.brbitmap, pmode,
  119.                 width,  height, pwidth, pheight,
  120.                 colortable, MIN(1<<depth,MAXAMCOLORREG), 4,    /* colors */ 
  121.         mskNone, 0,        /* masking. transColor */
  122.         NULL, NULL,        /* additional chunk lists */
  123.         ilbmname);
  124.         }
  125.  
  126.         for(k=0; k<depth; k++) 
  127.             {
  128.             if(ilbm.brbitmap->Planes[k])
  129.             FreeRaster(ilbm.brbitmap->Planes[k],width,height);
  130.         }
  131.     FreeMem(ilbm.brbitmap, sizeof(struct BitMap) + (extra << 2));
  132.     }
  133.  
  134.     Close(rawfile);
  135.  
  136.     if(error)
  137.     {
  138.     printf("%s\n",IFFerr(error));
  139.     bye(" ", RETURN_FAIL);
  140.     }
  141.     else bye("",RETURN_OK);
  142.     }
  143.  
  144.  
  145. void bye(UBYTE *s,int e)
  146.     {
  147.     if(s&&(*s)) printf("%s\n",s);
  148.     if ((fromWB)&&(*s))    /* Wait so user can read messages */
  149.         {
  150.         printf("\nPRESS RETURN TO EXIT\n");
  151.         while(getchar() != '\n');
  152.         }
  153.     cleanup();
  154.     exit(e);
  155.     }
  156.  
  157. void cleanup()
  158.     {
  159.     if(ilbm.ParseInfo.iff)    FreeIFF(ilbm.ParseInfo.iff);
  160.  
  161.     if(GfxBase)        CloseLibrary(GfxBase);
  162.     if(IntuitionBase)    CloseLibrary(IntuitionBase);
  163.     if(IFFParseBase)    CloseLibrary(IFFParseBase);
  164.     }
  165.  
  166.